package com.example.rpc.framework;

import com.example.rpc.framework.protocl.http.HttpClient;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory {

    //假设用户配置的是jdk，那么我们采用jdk代理方式实现
    public static <T> T getProxy(Class interfaceClass){
        return (T)Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[]{interfaceClass}, new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                HttpClient client=new HttpClient();//连接池代替
                Invocation invocation=new Invocation(interfaceClass.getName(),method.getName(),method.getParameterTypes(),args);
                return client.send("localhost",8080,invocation);// ip port 采用拉取注册中心实现
            }
        });
    }
}
